# 作者：陈睿-cruise
# 2025年02月19日16时36分28秒
# 2979386061@qq.com
import threading
import time

# 共享资源
counter = 0

# 创建一个 Lock 对象
lock = threading.Lock()

# 线程要执行的任务
def increment():
    global counter
    for _ in range(100000):
        # 获取锁
        lock.acquire()
        counter += 1
        # 释放锁
        lock.release()

def decrement():
    global counter
    for _ in range(100000):
        # 获取锁
        lock.acquire()
        counter -= 1
        # 释放锁
        lock.release()

if __name__ == "__main__":
    threads = []

    # 创建多个线程
    t1 = threading.Thread(target=increment)
    t2 = threading.Thread(target=decrement)

    threads.append(t1)
    threads.append(t2)

    # 启动线程
    t1.start()
    t2.start()

    # 等待所有线程完成
    for t in threads:
        t.join()

    print(f"Final counter value: {counter}")
